import React from 'react';
import { connect } from 'dva';
import { message } from 'antd';
import { ContentPackage, AdvancedSearch, DATA_ENTRY, TableC } from '../../comps';
import moment from 'moment';
import { exportFile } from '../../utils/commonFunc';

class PerformanceAnalysisReport extends React.Component {
  constructor() {
    super();
    this.state = {
      columns: [],
      dataSource: [],
      current: 1,
      pageSize: 10,
      total: 0,
      isReset: false, // 是否点击重置
      querys: {}, // 查询条件
    };
  }

  componentDidMount() {
    this.getTableData();
  }

  // 改变columns的样式
  changeColumns = (columns) => {
    if (columns && columns.length) {
      columns.map((item) => {
        if (item.key == 'operate') {
          item.operates = [
            {
              title: '导出',
              onClick: (record, index) => {
                this.export(record);
              },
            },
          ];
        }
      });
    }
    return columns;
  };

  // 导出文件
  export = (record) => {
    exportFile('gateway/file/maat-admin/file/download', record.fileList, true);
  };

  // 查询
  onQuery = (query) => {
    query.year && (query.year = moment(query.year).format('YYYY'));
    this.setState(
      {
        isReset: false,
        current: 1,
        querys: query,
      },
      () => {
        this.getTableData();
      },
    );
  };

  // 重置
  onClear = () => {
    this.setState({
      isReset: true,
    });
  };

  // 获取表格数据
  getTableData = () => {
    const {
      performanceAnalysisReportM: { columns },
    } = this.props;
    const { current, pageSize, querys } = this.state;
    this.setState(
      {
        columns: undefined,
        dataSource: undefined,
      },
      () => {
        this.props.dispatch({
          type: 'performanceAnalysisReportM/getDataList',
          payload: {
            ...querys,
            current,
            pageSize,
          },
          callback: (data) => {
            const newColumns = this.changeColumns(columns);
            if (data) {
              this.setState({
                columns: newColumns,
                dataSource: data.dataSource,
                current: data.pageInfo?.currentPage,
                pageSize: data.pageInfo?.numInPage,
                total: data.pageInfo?.total,
              });
            } else {
              this.setState({
                columns: newColumns,
                dataSource: [],
                current: 1,
                pageSize: 10,
                total: 0,
              });
            }
          },
        });
      },
    );
  };

  // 表格页码排序改变调用
  handleTableChange = (pagination) => {
    const { current, pageSize } = pagination;
    if (this.state.isReset) {
      // 判断是否点击了重置 点击页码不进行后台查询
      message.warning('查询条件已被重置，请重新查询。');
      return;
    }
    this.setState({ current, pageSize }, () => {
      this.getTableData();
    });
  };

  render() {
    const { columns, dataSource, current, pageSize, total } = this.state;
    return (
      <ContentPackage
        title='绩效分析报告'
        contentHtml={
          <div>
            <AdvancedSearch
              dataSource={[
                {
                  yearKey: 'year',
                  quarterKey: 'quater',
                  type: DATA_ENTRY.SIMPLEQUARTERC,
                  title: '报告时间',
                  highCurrentDate: true,
                  onQuarterChange: (data) => {},
                },
              ]}
              onQuery={this.onQuery}
              onClear={this.onClear}
            />
            <div style={{ marginTop: '15px' }}>
              <TableC
                columns={columns}
                dataSource={dataSource}
                rowKey='id'
                // scroll={{ x: 'max-content' }}
                selfScrollY={true}
                current={current}
                pageSize={pageSize}
                total={total}
                onChange={this.handleTableChange}
              />
            </div>
          </div>
        }
      />
    );
  }
}
export default connect(({ performanceAnalysisReportM }) => ({ performanceAnalysisReportM }))(PerformanceAnalysisReport);
